package com.rookie.shiro.module.config;

import com.rookie.shiro.module.constant.CommonJson;
import com.rookie.shiro.module.constant.TokenTypeEnum;
import com.rookie.shiro.module.util.JsonUtils;
import com.rookie.shiro.module.util.JwtUtils;
import com.rookie.shiro.module.util.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author rookie
 */
@Slf4j
public class ShiroFilter extends BasicHttpAuthenticationFilter {

    /**
     * 判断是否已经登录，返回true则为已登录状态则不会调用onAccessDenied，否则调用onAccessDenied执行登录
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        String errorJson = null;
        try {
            executeLogin(request, response);
            return true;
        } catch (IncorrectCredentialsException e){
            errorJson = JsonUtils.getJsonString(new CommonJson("500","token为空"));
            WebUtils.errorJson(errorJson, response);
        }catch (ExpiredCredentialsException e){
            errorJson = JsonUtils.getJsonString(new CommonJson("503","token失效"));
            WebUtils.errorJson(errorJson, response);
        }catch (Exception e) {
            errorJson = JsonUtils.getJsonString(new CommonJson("500","token异常"));
            WebUtils.errorJson(errorJson, response);
        }
        return false;
    }

    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) {
        String headToken = WebUtils.getToken(request);
        Token token = new Token(headToken);
        this.getSubject(request, response).login(token);
        return true;
    }

    /**
     *
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        return false;
    }

    /**
     * 为response设置header，实现跨域
     */
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
        // 跨域时会首先发送一个option请求，这里我们给option请求直接返回正常状态
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            httpServletResponse.setStatus(HttpStatus.OK.value());
            return false;
        }
        return super.preHandle(request, response);
    }

    public static void main(String[] args) {
        System.out.println(JwtUtils.sign("", TokenTypeEnum.PC.getMessage(),1));
    }
}